import type { AxiosResponse } from "axios";
import axios from "axios";

// 请求拦截
axios.interceptors.request.use((config: any) => {
  return config
}, (error) => {
  return error
})

// 响应拦截
axios.interceptors.response.use((config: any) => {
	 return config
},(error) => {
  return error
})

const request = <T>(url: string, options?: { [key: string]: any }) => {
  return new Promise<T>((resolve, reject) => {
    const option = {
      url,
      ...options,
      baseURL: "/api",
    }
    axios(option).then((res: AxiosResponse<T & { success: boolean, data: any, msg?: string, token?: string,  }>) => {
      const { success, } = res.data
      if (success) {
        resolve(res.data)
      } else {
        reject(res.data)
      }
    }).catch(error => {
      reject(error)
    })
  })
}

export {
  request
};

